/**
 *
 */
package com.angel.dao.generic.base;

import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;

import com.angel.dao.generic.comparators.FindAllInstancesActionPriorityComparator;
import com.angel.dao.generic.interfaces.FindAllInstancesAction;

/**
 *
 * @author William
 *
 */
public class InstancesActionManager<T extends Object> {

	private SortedSet<FindAllInstancesAction<T>> actions;

	public InstancesActionManager(){
		super();
		this.actions = new TreeSet<FindAllInstancesAction<T>>(new FindAllInstancesActionPriorityComparator<T>());
	}

	public boolean addInstancesAction(FindAllInstancesAction<T> action){
		return actions.add(action);
	}

	public void clearActions(){
		this.actions.clear();
	}

	public Collection<T> appliesActions(Collection<T> instances){
		Collection<T> resultCollection = instances;
		for(FindAllInstancesAction<T> action: actions){
			resultCollection = action.appliesAction(resultCollection);
		}
		return resultCollection;
	}
}
